草庐IT

Rust 结构体

全部标签

struct - 如何从函数访问结构的实例字段?

假设我有一个Graph结构,如下所示:typeGraphstruct{nodes[]intadjListmap[int][]int}//somemethodsonthestruct//constructorfuncNew()*Graph{g:=new(Graph)g.adjList=make(map[int][]int)returng}现在,我创建了该结构的一个新实例,其中:aGraph:=New()。如何访问Graph结构(aGraph)的这个特定实例的字段?换句话说,我如何访问aGraph版本的nodes数组(例如,从另一个顶级函数中)?非常感谢任何帮助!

go - 在 Go 中查找结构的底层匿名字段类型

我有这两个结构:typeCustomTimestruct{time.Time}typeEventsstruct{TimestampCustomTime}当我反射(reflect)Events.Timestamp的字段时,我得到CustomTime;我怎样才能得到实际的底层类型time.Time? 最佳答案 这是一个goplayground示例,展示了如何访问匿名字段。https://play.golang.org/p/yQULMVaQK0基本上,一旦您获得了结构的值,您就应该能够从字段0中获取时间值

sorting - 对具有公共(public)字段的不同结构进行排序的最佳解决方案

我有这样的结构类型typeAstruct{NamestringCreatedAttime.Time...}typeBstruct{TitlestringCreatedAttime.Time...}typeCstruct{MessagestringCreatedAttime.Time...}还有一个通用slicevarresult[]interface{}包含A、B和C元素(将来还会有更多元素)我想按“CreatedAt”对slice进行排序。什么是最好的解决方案?我想避免检查类型或转换... 最佳答案 无论如何,您可以拥有包含这两种

go - 继续-无法解析嵌套结构

Closed.ThisquestiondoesnotmeetStackOverflowguidelines。它当前不接受答案。想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。去年关闭。Improvethisquestion我有以下结构typelogsstruct{EmailstringAccountstringBranchNamestring`json:"branch_name"`TokenstringActions[]action}typeactionstruct{timestringnamestringdatastring}和代码解析funclogsHandl

arrays - 自己结构数组的JSON编码

我尝试读取一个目录并从文件条目中生成一个JSON字符串。但是json.encoder.Encode()函数只返回空对象。为了测试,我在tmp目录中有两个文件:test1.jstest2.jsgo程序是这样的:packagemainimport("encoding/json""fmt""os""path/filepath""time")typeFilestruct{namestringtimeStampint64}funcmain(){files:=make([]File,0,20)filepath.Walk("/home/michael/tmp/",func(pathstring,fo

Go Lang-将结构附加到 slice

是否可以将struct附加到slice?任何人都可以张贴一个例子吗?此slice需要具有struct值。testSlice=make([]Row,10)我试过用这种方式追加,但没有用。testSlice.append(row) 最佳答案 testSlice=append(testSlice,Row{/*...*/}) 关于GoLang-将结构附加到slice,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c

go - go中结构的内存分配

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion我最近遇到了http://golang-sizeof.tips/这解释了如何为结构分配内存。我知道为了确保连续的内存分配,我们在为没有填充的变量分配内存时添加填充将不会获得连续的内存。所以我在我的64位计算机上测试了各种组合,发现网站上的结果和我的计算机上的结果不匹配。这是针对这种情况的:typeS2struct{astringbboolebooldint32fboolcstring}主要是,以下

json - 将 "{}"主体解码为结构时,Golang 不会产生错误

在restapi中,当body设置为“{}”时,jsonDecoder不会产生错误。这使得有必要检查目标结构是否仍为nil。我需要检查库是否应该像这样工作,或者这是否是它的问题。//ClientSidethisrequestreq,err:=http.NewRequest("POST","url",strings.NewReader("{}"))//Curlequivalent:curl-XPOST-d'{}'http://api:8080/r/primitives/multiply//ServersidetypeOperandsstruct{Values[]float64`json:

go - 将结构保存到 json 时运行时 : goroutine stack exceeds 1000000000-byte limit,

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭3年前。Improvethisquestion我已经定义了一个gostruct的Trie数据结构。typeNodestruct{ValruneIsWordboolIsRootboolParent*NodeChildrenmap[rune]*Node}typeTriestruct{Root*Node}

Go vs Rust 垃圾收集器性能和类型

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭3年前。Improvethisquestion我阅读了一些关于Go的GC的堆栈溢出信息。Go垃圾收集器中的栈和堆模型我想知道如果一个变量需要在Go中定义在堆或栈中,然后GC要收集它,使用什么算法?如果我们假设在具有GC的语言中,堆更高效,那么Rust又如何?与Go相比,Rust如何处理这个问题?特别是关于Go中的引用计数器,我们在大多数时候别无选择地询问编译器,但这样的工具存在并且它以自己的方式完成它的工作!我读过这个:Stackvsheap